package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.FileDownloadService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import java.util.List;

/**
 *文件下载管理
 */
@RestController
@RequestMapping("/bk")
public class FileDownloadController {

    private static final Logger logger = LogManager.getLogger(FileDownloadController.class);

    @Resource
    private FileDownloadService fileDownloadService;

    @LoginRequired
    @PostMapping("/addFile")
    @ApiOperation(value = "新增文件")
    public JsonResult addFile(
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt,
            @ApiParam(value = "附件名称") @RequestParam(value = "attachName",required = false) String attachName,
            @ApiParam(value = "附件URL") @RequestParam(value = "attachUrl",required = false) String attachUrl
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("title", title);
            pageData.put("author", author);
            pageData.put("origin", origin);
            pageData.put("publicAt", publicAt);
            pageData.put("content", content);
            pageData.put("attachName", attachName);
            pageData.put("attachUrl", attachUrl);

            fileDownloadService.addFile(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateFile")
    @ApiOperation(value = "更新文件")
    public JsonResult updateFile(
            @ApiParam(value = "ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt,
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "附件名称") @RequestParam(value = "attachName",required = false) String attachName,
            @ApiParam(value = "附件URL") @RequestParam(value = "attachUrl",required = false) String attachUrl
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("title", title);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("origin", origin);
            pageData.put("content", content);
            pageData.put("attachName", attachName);
            pageData.put("attachUrl", attachUrl);

            fileDownloadService.updateFile(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteFile")
    @ApiOperation(value = "删除文件")
    public JsonResult deleteFile(
            @ApiParam(value = "文件ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids.split(","));

            fileDownloadService.deleteFile(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listFile")
    @ApiOperation(value = "获取文件列表")
    public JsonResult listFile(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(fileDownloadService.listFile(pageData));
            result.setResultNum(fileDownloadService.listFileCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


}
